<?php
/**
* @file $Id: MarkingPeriods.php 161 2006-09-07 06:21:17Z doritojones $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

DrawHeader(ProgramTitle());

if(!$_REQUEST['marking_period_id'] && count($fy_RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM SCHOOL_YEARS WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."' ORDER BY SORT_ORDER")))==1)
{
	$_REQUEST['marking_period_id'] = $fy_RET[1]['MARKING_PERIOD_ID'];
	$_REQUEST['mp_term'] = 'FY';
}

unset($_SESSION['_REQUEST_vars']['marking_period_id']);
unset($_SESSION['_REQUEST_vars']['mp_term']);

switch($_REQUEST['mp_term'])
{
	case 'FY':
		$table = 'SCHOOL_YEARS';
		if($_REQUEST['marking_period_id']=='new')
			$title = _('New Year');
	break;

	case 'SEM':
		$table = 'SCHOOL_SEMESTERS';
		if($_REQUEST['marking_period_id']=='new')
			$title = _('New Semester');
	break;

	case 'QTR':
		$table = 'SCHOOL_QUARTERS';
		if($_REQUEST['marking_period_id']=='new')
			$title = _('New Quarter');
	break;

	case 'PRO':
		$table = 'SCHOOL_PROGRESS_PERIODS';
		if($_REQUEST['marking_period_id']=='new')
			$title = _('New Progress Period');
	break;
}

// UPDATING
if($_REQUEST['day_tables'] && $_POST['day_tables'])
{
	foreach($_REQUEST['day_tables'] as $id=>$values)
	{
		if($_REQUEST['day_tables'][$id]['START_DATE'] && $_REQUEST['month_tables'][$id]['START_DATE'] && $_REQUEST['year_tables'][$id]['START_DATE'])
			$_REQUEST['tables'][$id]['START_DATE'] = $_REQUEST['day_tables'][$id]['START_DATE'].'-'.$_REQUEST['month_tables'][$id]['START_DATE'].'-'.$_REQUEST['year_tables'][$id]['START_DATE'];
		elseif(isset($_REQUEST['day_tables'][$id]['START_DATE']) && isset($_REQUEST['month_tables'][$id]['START_DATE']) && isset($_REQUEST['year_tables'][$id]['START_DATE']))
			$_REQUEST['tables'][$id]['START_DATE'] = '';

		if($_REQUEST['day_tables'][$id]['END_DATE'] && $_REQUEST['month_tables'][$id]['END_DATE'] && $_REQUEST['year_tables'][$id]['END_DATE'])
			$_REQUEST['tables'][$id]['END_DATE'] = $_REQUEST['day_tables'][$id]['END_DATE'].'-'.$_REQUEST['month_tables'][$id]['END_DATE'].'-'.$_REQUEST['year_tables'][$id]['END_DATE'];
		elseif(isset($_REQUEST['day_tables'][$id]['END_DATE']) && isset($_REQUEST['month_tables'][$id]['END_DATE']) && isset($_REQUEST['year_tables'][$id]['END_DATE']))
			$_REQUEST['tables'][$id]['END_DATE'] = '';

		if($_REQUEST['day_tables'][$id]['POST_START_DATE'] && $_REQUEST['month_tables'][$id]['POST_START_DATE'] && $_REQUEST['year_tables'][$id]['POST_START_DATE'])
			$_REQUEST['tables'][$id]['POST_START_DATE'] = $_REQUEST['day_tables'][$id]['POST_START_DATE'].'-'.$_REQUEST['month_tables'][$id]['POST_START_DATE'].'-'.$_REQUEST['year_tables'][$id]['POST_START_DATE'];
		elseif(isset($_REQUEST['day_tables'][$id]['POST_START_DATE']) && isset($_REQUEST['month_tables'][$id]['POST_START_DATE']) && isset($_REQUEST['year_tables'][$id]['POST_START_DATE']))
			$_REQUEST['tables'][$id]['POST_START_DATE'] = '';

		if($_REQUEST['day_tables'][$id]['POST_END_DATE'] && $_REQUEST['month_tables'][$id]['POST_END_DATE'] && $_REQUEST['year_tables'][$id]['POST_END_DATE'])
			$_REQUEST['tables'][$id]['POST_END_DATE'] = $_REQUEST['day_tables'][$id]['POST_END_DATE'].'-'.$_REQUEST['month_tables'][$id]['POST_END_DATE'].'-'.$_REQUEST['year_tables'][$id]['POST_END_DATE'];
		elseif(isset($_REQUEST['day_tables'][$id]['POST_END_DATE']) && isset($_REQUEST['month_tables'][$id]['POST_END_DATE']) && isset($_REQUEST['year_tables'][$id]['POST_END_DATE']))
			$_REQUEST['tables'][$id]['POST_END_DATE'] = '';

		if($_REQUEST['day_tables'][$id]['REGISTRATION_START_DATE'] && $_REQUEST['month_tables'][$id]['REGISTRATION_START_DATE'] && $_REQUEST['year_tables'][$id]['REGISTRATION_START_DATE'])
			$_REQUEST['tables'][$id]['REGISTRATION_START_DATE'] = $_REQUEST['day_tables'][$id]['REGISTRATION_START_DATE'].'-'.$_REQUEST['month_tables'][$id]['REGISTRATION_START_DATE'].'-'.$_REQUEST['year_tables'][$id]['REGISTRATION_START_DATE'];
		elseif(isset($_REQUEST['day_tables'][$id]['REGISTRATION_START_DATE']) && isset($_REQUEST['month_tables'][$id]['REGISTRATION_START_DATE']) && isset($_REQUEST['year_tables'][$id]['REGISTRATION_START_DATE']))
			$_REQUEST['tables'][$id]['REGISTRATION_START_DATE'] = '';

		if($_REQUEST['day_tables'][$id]['REGISTRATION_END_DATE'] && $_REQUEST['month_tables'][$id]['REGISTRATION_END_DATE'] && $_REQUEST['year_tables'][$id]['REGISTRATION_END_DATE'])
			$_REQUEST['tables'][$id]['REGISTRATION_END_DATE'] = $_REQUEST['day_tables'][$id]['REGISTRATION_END_DATE'].'-'.$_REQUEST['month_tables'][$id]['REGISTRATION_END_DATE'].'-'.$_REQUEST['year_tables'][$id]['REGISTRATION_END_DATE'];
		elseif(isset($_REQUEST['day_tables'][$id]['REGISTRATION_END_DATE']) && isset($_REQUEST['month_tables'][$id]['REGISTRATION_END_DATE']) && isset($_REQUEST['year_tables'][$id]['REGISTRATION_END_DATE']))
			$_REQUEST['tables'][$id]['REGISTRATION_END_DATE'] = '';
	}
	if(!$_POST['tables'])
		$_POST['tables'] = $_REQUEST['tables'];
}

if($_REQUEST['tables'] && $_POST['tables'] && AllowEdit())
{
	foreach($_REQUEST['tables'] as $id=>$columns)
	{
		if($id!='new')
		{
			$sql = "UPDATE $table SET ";

			foreach($columns as $column=>$value)
			{
				if($column=='START_DATE' || $column=='END_DATE' || $column=='POST_START_DATE' || $column=='POST_END_DATE' || $column=='REGISTRATION_START_DATE' || $column=='REGISTRATION_END_DATE')
		 		{
					if(!VerifyDate($value) && $value!='')
			 			BackPrompt(_('Not all of the dates were entered correctly.'));
				}
				$sql .= $column."='".str_replace("\'","''",$value)."',";
			}
			$sql = substr($sql,0,-1) . " WHERE MARKING_PERIOD_ID='$id'";
			$go = true;
		}
		else
		{
			$errors = array();
			if(!$columns['TITLE'])
				$errors[] = _('Title');
			if(!$columns['SHORT_NAME'])
				$errors[] = _('Short Name');
			if($_REQUEST['mp_term']!='PRO' && !$columns['YEAR_FRACTION'])
				$errors[] = _('Year Fraction');

			if(count($errors)==1)
				BackPrompt(_('The').' '.$errors[0].' '._('is required.'));
			elseif(count($errors)==2)
				BackPrompt(_('The').' '.$errors[0].' '._('and the').' '.$errors[1].' '._('are required.'));
			elseif(count($errors)==3)
				BackPrompt(_('The').' '.$errors[0].', '._('the').' '.$errors[1].', '._('and the').' '.$errors[2].' '._('are required.'));


			$id_RET = DBGet(DBQuery('SELECT '.db_seq_nextval('MARKING_PERIOD_SEQ').' AS ID'.FROM_DUAL));

			$sql = "INSERT INTO $table ";
			$fields = "MARKING_PERIOD_ID,SYEAR,SCHOOL_ID,";
			$values = "'".$id_RET[1]['ID']."','".UserSyear()."','".UserSchool()."',";

			$_REQUEST['marking_period_id'] = $id_RET[1]['ID'];

			if($_REQUEST['mp_term']=='SEM')
			{
				$fields .= "YEAR_ID,";
				$values .= "'$_REQUEST[year_id]',";
			}
			elseif($_REQUEST['mp_term']=='QTR')
			{
				$fields .= "SEMESTER_ID,";
				$values .= "'$_REQUEST[semester_id]',";
			}
			elseif($_REQUEST['mp_term']=='PRO')
			{
				$fields .= "QUARTER_ID,";
				$values .= "'$_REQUEST[quarter_id]',";
			}

			$go = false;
			foreach($columns as $column=>$value)
			{
				if($column=='START_DATE' || $column=='END_DATE' || $column=='POST_START_DATE' || $column=='POST_END_DATE' || $column=='REGISTRATION_START_DATE' || $column=='REGISTRATION_END_DATE')
		 		{
					if(!VerifyDate($value) && $value!='')
			 			BackPrompt(_('Not all of the dates were entered correctly.'));
				}
				if($value)
				{
					$fields .= $column.',';
					$values .= "'".str_replace("\'","''",$value)."',";
					$go = true;
				}
			}
			$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
		}

		// CHECK TO MAKE SURE ONLY ONE MP & ONE GRADING PERIOD IS OPEN AT ANY GIVEN TIME
		$dates_RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM $table WHERE (true=false"
			.(($columns['START_DATE'])?" OR '".$columns['START_DATE']."' BETWEEN START_DATE AND END_DATE":'')
			.(($columns['END_DATE'])?" OR '".$columns['END_DATE']."' BETWEEN START_DATE AND END_DATE":'')
			.(($columns['START_DATE'] && $columns['END_DATE'])?" OR START_DATE BETWEEN '".$columns['START_DATE']."' AND '".$columns['END_DATE']."'
			OR END_DATE BETWEEN '".$columns['START_DATE']."' AND '".$columns['END_DATE']."'":'')
			.") AND SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."'".(($id!='new')?" AND SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."' AND MARKING_PERIOD_ID!='$id'":'')
		));
		$posting_RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM $table WHERE (true=false"
			.(($columns['POST_START_DATE'])?" OR '".$columns['POST_START_DATE']."' BETWEEN POST_START_DATE AND POST_END_DATE":'')
			.(($columns['POST_END_DATE'])?" OR '".$columns['POST_END_DATE']."' BETWEEN POST_START_DATE AND POST_END_DATE":'')
			.(($columns['POST_START_DATE'] && $columns['POST_END_DATE'])?" OR POST_START_DATE BETWEEN '".$columns['POST_START_DATE']."' AND '".$columns['POST_END_DATE']."'
			OR POST_END_DATE BETWEEN '".$columns['POST_START_DATE']."' AND '".$columns['POST_END_DATE']."'":'')
			.") AND SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."'".(($id!='new')?" AND MARKING_PERIOD_ID!='$id'":'')
		));

		if(count($dates_RET))
			BackPrompt(_('The beginning and end dates you specified for this marking period overlap with those of').' '.GetMP($dates_RET[1]['MARKING_PERIOD_ID']).".  "._('Only one marking period can be open at any time.'));
		if(count($posting_RET))
			BackPrompt(_('The grade posting dates you specified for this marking period overlap with those of').' '.GetMP($posting_RET[1]['MARKING_PERIOD_ID']).".  "._('Only one grade posting period can be open at any time.'));


		if($go)
			DBQuery($sql);
	}
	unset($_REQUEST['tables']);
	unset($_SESSION['_REQUEST_vars']['tables']);
}

if($_REQUEST['modfunc']=='delete')
{
	$extra = array();
	switch($table)
	{
		case 'SCHOOL_YEARS':
			$name = _('year');
			$parent_term = ''; $parent_id = '';
			$extra[] = "DELETE FROM SCHOOL_PROGRESS_PERIODS WHERE QUARTER_ID IN (SELECT MARKING_PERIOD_ID FROM SCHOOL_QUARTERS WHERE SEMESTER_ID IN (SELECT MARKING_PERIOD_ID FROM SCHOOL_SEMESTERS WHERE YEAR_ID='$_REQUEST[marking_period_id]'))";
			$extra[] = "DELETE FROM SCHOOL_QUARTERS WHERE SEMESTER_ID IN (SELECT MARKING_PERIOD_ID FROM SCHOOL_SEMESTERS WHERE YEAR_ID='$_REQUEST[marking_period_id]')";
			$extra[] = "DELETE FROM SCHOOL_SEMESTERS WHERE YEAR_ID='$_REQUEST[marking_period_id]'";
		break;

		case 'SCHOOL_SEMESTERS':
			$name = _('semester');
			$parent_term = 'FY'; $parent_id = $_REQUEST['year_id'];
			$extra[] = "DELETE FROM SCHOOL_PROGRESS_PERIODS WHERE QUARTER_ID IN (SELECT MARKING_PERIOD_ID FROM SCHOOL_QUARTERS WHERE SEMESTER_ID='$_REQUEST[marking_period_id]')";
			$extra[] = "DELETE FROM SCHOOL_QUARTERS WHERE SEMESTER_ID='$_REQUEST[marking_period_id]'";
		break;

		case 'SCHOOL_QUARTERS':
			$name = _('quarter');
			$parent_term = 'SEM'; $parent_id = $_REQUEST['semester_id'];
			$extra[] = "DELETE FROM SCHOOL_PROGRESS_PERIODS WHERE QUARTER_ID='$_REQUEST[marking_period_id]'";
		break;

		case 'SCHOOL_PROGRESS_PERIODS':
			$name = _('progress period');
			$parent_term = 'QTR'; $parent_id = $_REQUEST['quarter_id'];
		break;
	}

	if(DeletePrompt($name))
	{
		foreach($extra as $sql)
			DBQuery($sql);
		DBQuery("DELETE FROM $table WHERE MARKING_PERIOD_ID='$_REQUEST[marking_period_id]'");
		unset($_REQUEST['modfunc']);
		$_REQUEST['mp_term'] = $parent_term;
		$_REQUEST['marking_period_id'] = $parent_id;
	}
	unset($extra);
	unset($_SESSION['_REQUEST_vars']['modfunc']);
}

if(!$_REQUEST['modfunc'])
{
	if($_REQUEST['marking_period_id']!='new' && $_REQUEST['mp_term']!='FY')
		$delete_button = "<INPUT type=button value="._('Delete')." onClick='javascript:window.location=\"Modules.php?modname=$_REQUEST[modname]&modfunc=delete&mp_term=$_REQUEST[mp_term]&year_id=$_REQUEST[year_id]&semester_id=$_REQUEST[semester_id]&quarter_id=$_REQUEST[quarter_id]&marking_period_id=$_REQUEST[marking_period_id]\"'>";

	// ADDING & EDITING FORM
	if($_REQUEST['marking_period_id'] && $_REQUEST['marking_period_id']!='new')
	{
		if($table!='SCHOOL_PROGRESS_PERIODS')
			$extra = ',REGISTRATION_START_DATE,REGISTRATION_END_DATE,YEAR_FRACTION';
		$sql = "SELECT TITLE,SHORT_NAME,SORT_ORDER,
						START_DATE,END_DATE,POST_START_DATE,POST_END_DATE".$extra."
				FROM $table
				WHERE MARKING_PERIOD_ID='$_REQUEST[marking_period_id]'";
		$QI = DBQuery($sql);
		$RET = DBGet($QI);
		$RET = $RET[1];
		$title = $RET['TITLE'];
		$div = true;
	}
	elseif($_REQUEST['marking_period_id']=='new')
	{
		$div = false;
		if($_REQUEST['mp_term']=='FY')
			$RET['YEAR_FRACTION'] = '1';
		elseif($_REQUEST['mp_term']=='SEM')
			$RET['YEAR_FRACTION'] = '.5';
		elseif($_REQUEST['mp_term']=='QTR')
			$RET['YEAR_FRACTION'] = '.25';
	}

	if($_REQUEST['marking_period_id'])
	{
		echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&mp_term=$_REQUEST[mp_term]&marking_period_id=$_REQUEST[marking_period_id]&year_id=$_REQUEST[year_id]&semester_id=$_REQUEST[semester_id]&quarter_id=$_REQUEST[quarter_id] method=POST>";
		DrawHeader('<B>'.$title.'</B>',$delete_button.'<INPUT type=submit value='._('Save').'>');
		$width = 'width=100%';

		$header .= '<TABLE style="display:inline;" cellpadding=5 '.$width.'>';
		$header .= '<TR>';

		$header .= '<TD>' . TextInput($RET['TITLE'],'tables['.$_REQUEST['marking_period_id'].'][TITLE]',_('Title')) . '</TD>';
		$header .= '<TD>' . TextInput($RET['SHORT_NAME'],'tables['.$_REQUEST['marking_period_id'].'][SHORT_NAME]',_('Short Name'),'maxlength=10') . '</TD>';
		$header .= '<TD>' . TextInput($RET['SORT_ORDER'],'tables['.$_REQUEST['marking_period_id'].'][SORT_ORDER]',_('Sort Order'),'size=3') . '</TD>';
		if($_REQUEST['mp_term']!='PRO')
			$header .= '<TD>' . TextInput($RET['YEAR_FRACTION'],'tables['.$_REQUEST['marking_period_id'].'][YEAR_FRACTION]',_('Fraction of Full Year'),'size=3',$div) . '</TD>';

		$header .= '</TR>';
		$header .= '</TABLE><TABLE cellpadding=3>';
		$header .= '<TR>';

		$header .= '<TD>' . DateInput($RET['START_DATE'],'tables['.$_REQUEST['marking_period_id'].'][START_DATE]',_('Begins')) . '</TD>';
		$header .= '<TD>' . DateInput($RET['END_DATE'],'tables['.$_REQUEST['marking_period_id'].'][END_DATE]',_('Ends')) . '</TD>';

		$header .= '</TR>';
		$header .= '<TR>';

		$header .= '<TD>' . DateInput($RET['POST_START_DATE'],'tables['.$_REQUEST['marking_period_id'].'][POST_START_DATE]',_('Grade Posting Begins')) . '</TD>';
		$header .= '<TD>' . DateInput($RET['POST_END_DATE'],'tables['.$_REQUEST['marking_period_id'].'][POST_END_DATE]',_('Grade Posting Ends')) . '</TD>';

		if($_REQUEST['mp_term']!='PRO')
		{
			$header .= '</TR>';
			$header .= '<TR>';

			$header .= '<TD>' . DateInput($RET['REGISTRATION_START_DATE'],'tables['.$_REQUEST['marking_period_id'].'][REGISTRATION_START_DATE]',_('Course Registration Begins')) . '</TD>';
			$header .= '<TD>' . DateInput($RET['REGISTRATION_END_DATE'],'tables['.$_REQUEST['marking_period_id'].'][REGISTRATION_END_DATE]',_('Course Registration Ends')) . '</TD>';
		}

		$header .= '</TR>';
		$header .= '</TABLE>';
		DrawHeader($header);
		echo '</FORM>';
		unset($_SESSION['_REQUEST_vars']['marking_period_id']);
		unset($_SESSION['_REQUEST_vars']['mp_term']);
	}

	// DISPLAY THE MENU
	$LO_options = array('save'=>false,'search'=>false);

	echo '<TABLE width=100%><TR>';

	// FY
	$sql = "SELECT MARKING_PERIOD_ID,TITLE FROM SCHOOL_YEARS WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."' ORDER BY SORT_ORDER";
	$QI = DBQuery($sql);
	$fy_RET = DBGet($QI);

	if(count($fy_RET))
	{
		if($_REQUEST['mp_term'])
		{
			if($_REQUEST['mp_term']=='FY')
				$_REQUEST['year_id'] = $_REQUEST['marking_period_id'];

			foreach($fy_RET as $key=>$value)
			{
				if($value['MARKING_PERIOD_ID']==$_REQUEST['year_id'])
					$fy_RET[$key]['row_color'] = Preferences('HIGHLIGHT');
			}
		}
	}

	echo '<TD valign=top width=25%>';
	$columns = array('TITLE'=>_('Year'));
	$link = array();
	$link['TITLE']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=$_REQUEST[modfunc]&mp_term=FY";
	$link['TITLE']['variables'] = array('marking_period_id'=>'MARKING_PERIOD_ID');
	if(!count($fy_RET))
		$link['add']['link'] = "Modules.php?modname=$_REQUEST[modname]&mp_term=FY&marking_period_id=new";

	ListOutput($fy_RET,$columns,_('Year'),_('Years'),$link,array(),$LO_options);
	echo '</TD>';

	// SEMESTERS
	if(($_REQUEST['mp_term']=='FY' && $_REQUEST['marking_period_id']!='new') || $_REQUEST['mp_term']=='SEM' || $_REQUEST['mp_term']=='QTR' || $_REQUEST['mp_term']=='PRO')
	{
		$sql = "SELECT MARKING_PERIOD_ID,TITLE FROM SCHOOL_SEMESTERS WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."' AND YEAR_ID='".$_REQUEST['year_id']."' ORDER BY SORT_ORDER";
		$QI = DBQuery($sql);
		$sem_RET = DBGet($QI);

		if(count($sem_RET))
		{
			if($_REQUEST['mp_term'])
			{
				if($_REQUEST['mp_term']=='SEM')
					$_REQUEST['semester_id'] = $_REQUEST['marking_period_id'];

				foreach($sem_RET as $key=>$value)
				{
					if($value['MARKING_PERIOD_ID']==$_REQUEST['semester_id'])
						$sem_RET[$key]['row_color'] = Preferences('HIGHLIGHT');
				}
			}
		}

		echo '<TD valign=top width=25%>';
		$columns = array('TITLE'=>_('Semester'));
		$link = array();
		$link['TITLE']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=$_REQUEST[modfunc]&mp_term=SEM&year_id=$_REQUEST[year_id]";
		$link['TITLE']['variables'] = array('marking_period_id'=>'MARKING_PERIOD_ID');
		$link['add']['link'] = "Modules.php?modname=$_REQUEST[modname]&mp_term=SEM&marking_period_id=new&year_id=$_REQUEST[year_id]";

		ListOutput($sem_RET,$columns,_('Semester'),_('Semesters'),$link,array(),$LO_options);
		echo '</TD>';

		// QUARTERS
		if(($_REQUEST['mp_term']=='SEM' && $_REQUEST['marking_period_id']!='new') || $_REQUEST['mp_term']=='QTR' || $_REQUEST['mp_term']=='PRO')
		{
			$sql = "SELECT MARKING_PERIOD_ID,TITLE FROM SCHOOL_QUARTERS WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."' AND SEMESTER_ID='".$_REQUEST['semester_id']."' ORDER BY SORT_ORDER";
			$QI = DBQuery($sql);
			$qtr_RET = DBGet($QI);

			if(count($qtr_RET))
			{
				if(($_REQUEST['mp_term']=='QTR' && $_REQUEST['marking_period_id']!='new') || $_REQUEST['mp_term']=='PRO')
				{
					if($_REQUEST['mp_term']=='QTR')
						$_REQUEST['quarter_id'] = $_REQUEST['marking_period_id'];

					foreach($qtr_RET as $key=>$value)
					{
						if($value['MARKING_PERIOD_ID']==$_REQUEST['quarter_id'])
							$qtr_RET[$key]['row_color'] = Preferences('HIGHLIGHT');
					}
				}
			}

			echo '<TD valign=top width=25%>';
			$columns = array('TITLE'=>_('Quarter'));
			$link = array();
			$link['TITLE']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=$_REQUEST[modfunc]&mp_term=QTR&year_id=$_REQUEST[year_id]&semester_id=$_REQUEST[semester_id]";
			$link['TITLE']['variables'] = array('marking_period_id'=>'MARKING_PERIOD_ID');
			$link['add']['link'] = "Modules.php?modname=$_REQUEST[modname]&mp_term=QTR&marking_period_id=new&year_id=$_REQUEST[year_id]&semester_id=$_REQUEST[semester_id]";

			ListOutput($qtr_RET,$columns,_('Quarter'),_('Quarters'),$link,array(),$LO_options);

			echo '</TD>';

			// PROGRESS PERIODS
			if(($_REQUEST['mp_term']=='QTR' && $_REQUEST['marking_period_id']!='new') || $_REQUEST['mp_term']=='PRO')
			{
				$sql = "SELECT MARKING_PERIOD_ID,TITLE FROM SCHOOL_PROGRESS_PERIODS WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."' AND QUARTER_ID='".$_REQUEST['quarter_id']."' ORDER BY SORT_ORDER";
				$QI = DBQuery($sql);
				$pro_RET = DBGet($QI);

				if(count($pro_RET))
				{
					if(($_REQUEST['mp_term']=='PRO' && $_REQUEST['marking_period_id']!='new'))
					{
						$_REQUEST['progress_period_id'] = $_REQUEST['marking_period_id'];

						foreach($pro_RET as $key=>$value)
						{
							if($value['MARKING_PERIOD_ID']==$_REQUEST['marking_period_id'])
								$pro_RET[$key]['row_color'] = Preferences('HIGHLIGHT');
						}
					}
				}

				echo '<TD valign=top width=25%>';
				$columns = array('TITLE'=>_('Progress Period'));
				$link = array();
				$link['TITLE']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=$_REQUEST[modfunc]&mp_term=PRO&year_id=$_REQUEST[year_id]&semester_id=$_REQUEST[semester_id]&quarter_id=$_REQUEST[quarter_id]";
				$link['TITLE']['variables'] = array('marking_period_id'=>'MARKING_PERIOD_ID');
				$link['add']['link'] = "Modules.php?modname=$_REQUEST[modname]&mp_term=PRO&marking_period_id=new&year_id=$_REQUEST[year_id]&semester_id=$_REQUEST[semester_id]&quarter_id=$_REQUEST[quarter_id]";

				ListOutput($pro_RET,$columns,_('Progress Period'),_('Progress Periods'),$link,array(),$LO_options);

				echo '</TD>';
			}
			else
				echo '<TD width=25%></TD>';
		}
		else
			echo '<TD width=25%></TD><TD width=25%></TD>';
	}
	else
		echo '<TD width=25%></TD><TD width=25%></TD><TD width=25%></TD>';

	echo '</TR></TABLE>';
}
?>